const { app, BrowserWindow, ipcMain, webContents, dialog, systemPreferences, desktopCapturer } = require('electron')
const path = require('path')
const fs = require('fs')
// 判断系统
const isMac = process.platform === 'darwin'

// 当前进程不受信任, 通过提示通知用户  如果当前进程是受信任的无障碍客户端，则为true，否则为 false
// if (isMac) systemPreferences.isTrustedAccessibilityClient(true)
// 屏幕访问权限
let screenAuthority = systemPreferences.getMediaAccessStatus('screen')
// 相机访问权限
let cameraAuthority = systemPreferences.getMediaAccessStatus('camera')

process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'

// 本地启动的vue项目路径
const vueProjectAddress = 'http://localhost:5173/#'
/**
 * 创建一个新的窗口
 */

const createWindow = () => {
	let win = new BrowserWindow({
		minWidth: 1000,
		minHeight: 600,
		width: 1000,
		height: 600,
		frame: false, // 窗口无边框
		titleBarStyle: 'hidden',
		webPreferences: {
			nodeIntegration: true, // 渲染进程使用Node API
			contextIsolation: false, // 是否开启隔离上下文
			webSecurity: false
		}
	})

	if (process.env.NODE_ENV != 'development') {
		win.loadFile(path.join(__dirname, '../dist/index.html'))
	} else {
		win.loadURL(vueProjectAddress)
	}

	win.webContents.openDevTools()

	let videoWin
	ipcMain.on('open-video', (e, url: string) => {
		if (videoWin) {
			videoWin.show()
		} else {
			videoWin = new BrowserWindow({
				width: 800,
				height: 500,
				titleBarStyle: 'hidden',
				resizable: false, // 窗口大小是否可以调整
				webPreferences: {
					nodeIntegration: true,
					contextIsolation: false
				}
			})

			videoWin.on('ready-to-show', () => {
				videoWin.show()
			})

			videoWin.on('close', () => {
				videoWin = null
			})
		}

		console.log(url)
		const URL = 'http://localhost:5173/' + 'video?url=' + url
		videoWin.loadURL(URL)
	})

	// 关闭视频窗口
	ipcMain.on('close-video', () => {
		videoWin.close()
		videoWin = null
	})

	// 最小化
	ipcMain.on('minWin', () => {
		win.minimize()
	})

	// 关闭
	ipcMain.on('close', () => {
		// 关闭软件
		app.quit()
	})
}

// 打开窗口
app.whenReady().then(() => {
	createWindow() // 创建窗口

	app.on('activate', () => {
		// if (BrowserWindow.getAllWindows().length === 0) createWindow()
	})
})

// 关闭窗口
app.on('window-all-closed', () => {
	app.quit()
	// if (process.platform !== "darwin") {
	//   app.quit()
	// }
})
